package com.example.shixun.service.impl;

import com.example.shixun.dao.StudentDao;
import com.example.shixun.pojo.Result;
import com.example.shixun.pojo.Student;
import com.example.shixun.service.StudentService;
import com.example.shixun.utils.PageInfoUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public  class StudentServiceImpl implements StudentService {
    @Resource
    StudentDao studentDao;
    @Override
    public PageInfoUtil<Student> getStudentList(PageInfoUtil<Student> pageInfoUtil){
        //该语句一定要在执行sql 语句之前，参数第一个参数代表当前第几页，第二个参数代表每页的数量
        PageHelper.startPage(pageInfoUtil.getPageNum(),pageInfoUtil.getPageSize());
        //System.out.println(pageInfoUtil.getPageSize());
        List<Student> lists=this.studentDao.getStudentList(pageInfoUtil.getParams());

        PageInfo<Student>  pageInfo=  new PageInfo<Student>(lists);
        System.out.println(pageInfo);
        pageInfoUtil.setPageNum(pageInfo.getPageNum());
        pageInfoUtil.setPageSize(pageInfo.getPageSize());
        pageInfoUtil.setPageTotal(pageInfo.getPages());
        pageInfoUtil.setTotal(pageInfo.getTotal());
        pageInfoUtil.setList(lists);
        return pageInfoUtil;
    }

    @Override
    public Result<Integer> AddStudent(Student stu) {
        if(stu.getUserName()!=null){
            int n=studentDao.saveStudent(stu);
            return Result.success(n);
        }
        return Result.fail("注册失败");
    }

    @Override
    public void deleteById(Integer id) {
        studentDao.deleteById(id);
    }
}
